﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TLD_WFC {
    public partial class MyPictureBox : PictureBox {
        public TLD_Vision.Image _img;

        public TLD_Vision.Image img {
            get {
                return _img;
            }

            set {
                _img = value;
                Image = _img != null ? img.GetBitmap() : null;
            }
        }

        public MyPictureBox()
            : base() {
            InitializeComponent();
            DoubleClick += new System.EventHandler(OpenImage);
            addContextMenus();
        }

        /// <summary>
        /// Adds context menus to the pictureboxes.
        /// </summary>
        private void addContextMenus() {
            ContextMenu contextMenu = new ContextMenu();
            this.ContextMenu = contextMenu;

            MenuItem itemOpen = new MenuItem();
            MenuItem itemView = new MenuItem();
            MenuItem itemSave = new MenuItem();

            itemOpen.Text = "&Open";
            itemView.Text = "&View";
            itemSave.Text = "&Save";

            contextMenu.MenuItems.Add(itemOpen);
            contextMenu.MenuItems.Add(itemView);
            contextMenu.MenuItems.Add(itemSave);

            itemOpen.Click += new EventHandler(OpenImage);
            itemView.Click += new EventHandler(ViewImage);
            itemSave.Click += new EventHandler(SaveImage);

        }

        /// <summary>
        /// Opens a OpenFileDialog to load image.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenImage(object sender, EventArgs e) {
            OpenFileDialog dlg = new OpenFileDialog();

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                img = new TLD_Vision.Image(dlg.FileName);
            }
        }

        /// <summary>
        /// Opens a view window to enlarge the image.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ViewImage(object sender, EventArgs e) {
            if (Image != null) {
                Form ipForm = new ImagePreviewForm((Bitmap)Image);
                ipForm.Show();
            }
        }

        /// <summary>
        /// Opens a SaveFileDialog to save the image.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveImage(object sender, EventArgs e) {
			if(img == null)
			{
				return;
			}

			SaveFileDialog sFD = new SaveFileDialog();
            sFD.Filter = "Image Files(*.PNG;)|*.PNG;";
            sFD.FilterIndex = 1;
            sFD.RestoreDirectory = true;

            Bitmap b = img.GetBitmap();

            if (sFD.ShowDialog() == DialogResult.OK
                && sFD.FileName != ""
                && b != null) {

                System.IO.FileStream fs = (System.IO.FileStream)sFD.OpenFile();
                b.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
                fs.Close();
            }
        }
    }
}
